package hyl.base.mq.rocket;

import java.io.IOException;
import java.util.Map;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import hyl.core.MyFun;
import hyl.core.conf.MyConfig;

public class RktConfig {

	private String nameServer = "localhost:9092";// 默认值
	// private static String _zkAddr = "localhost:9092";
	private String group = "test";
	private static Map<String, Map<String, String>> _confs = null;

	private ConsumeFromWhere consumeFrom = ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET;
	private int retryTimeFailed = 0;
	private int delayTimeLevel = 1;
	private int 每批最多拉取 = 20;

	private int 缓存队列消息 = 2000;
	private int 最少线程 = 16;

	private int 最多线程 = 32;

	private MessageModel msgModel = MessageModel.BROADCASTING;

	public static RktConfig getInstance(String 配置, String group) throws IOException {
		if (_confs == null)
			_confs = MyConfig.loadJson("mq/rocketmq.conf", null); //这么写是因为 生产和销售共用一个配置文件

		RktConfig _conf = new RktConfig();
		
		Map<String, String> prpp = _confs.get(配置);
		_conf.setNameServer(prpp.get("nameserver"));
		_conf.setGroup(group);
		_conf.setRetryTimeFailed(prpp.get("retrytime"));
		_conf.setConsumeFrom(prpp.get("fromwhere"));
		_conf.setDelayTimeLevel(prpp.get("delaytime"));
		_conf.set每批最多拉取(prpp.get("每批最多拉取"));
		_conf.set缓存队列消息(prpp.get("缓存队列消息"));

		_conf.set消费模式(prpp.get("消费模式"));// 默认集群模式
		_conf.set最少线程(prpp.get("最少线程"));
		_conf.set最多线程(prpp.get("最多线程"));
		System.out.println("rocketmq 配置已加载完成..");
		return _conf;
	}

	public int get缓存队列消息() {
		return 缓存队列消息;
	}

	public void set缓存队列消息(String 数量) {
		if (!MyFun.isEmpty(数量))
			this.缓存队列消息 = Integer.parseInt(数量);
	}

	public int get最少线程() {
		return 最少线程;
	}

	public void set最少线程(String 数量) {
		if (!MyFun.isEmpty(数量))
			this.最少线程 = Integer.parseInt(数量);
	}

	public int get最多线程() {
		return 最多线程;
	}

	public void set最多线程(String 数量) {
		if (!MyFun.isEmpty(数量))
			this.最多线程 = Integer.parseInt(数量);
	}

	// comsumefromwhere
	// CONSUME_FROM_LAST_OFFSET 从上一次消费开始
	// CONSUME_FROM_FIRST_OFFSET ，从头开始消费
	// CONSUME_FROM_TIMESTAMP ，从指定时间开始
	/**
	 * @return the nameServer
	 */
	public String getNameServer() {
		return nameServer;
	}

	/**
	 * @param nameServer the nameServer to set
	 */
	public void setNameServer(String nameServer) {
		if (!MyFun.isEmpty(nameServer))
			this.nameServer = nameServer;
	}

	/**
	 * @return the group
	 */
	public String getGroup() {
		return this.group;
	}

	/**
	 * @param group the group to set
	 */
	public void setGroup(String group) {
		if (!MyFun.isEmpty(group))
			this.group = group;
	}

	/**
	 * @return the consumeFrom
	 */
	public ConsumeFromWhere getConsumeFrom() {

		return consumeFrom;
	}

	/**
	 * @param consumeFrom the consumeFrom to set
	 */
	public void setConsumeFrom(String consumeFrom) {
		if (MyFun.isEmpty(consumeFrom))
			this.consumeFrom = ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET;
		else
			this.consumeFrom = ConsumeFromWhere.valueOf(consumeFrom);

	}

	/**
	 * @return the retryTimeFailed
	 */
	public int getRetryTimeFailed() {
		return retryTimeFailed;
	}

	/**
	 * @param retryTimeFailed the retryTimeFailed to set
	 */
	public void setRetryTimeFailed(String retryTimeFailed) {
		if (!MyFun.isEmpty(retryTimeFailed))
			this.retryTimeFailed = Integer.parseInt(retryTimeFailed);
	}

	/**
	 * @return the delayTimeLevel
	 */
	public int getDelayTimeLevel() {
		return delayTimeLevel;
	}

	/**
	 * @param delayTimeLevel the delayTimeLevel to set
	 */
	public void setDelayTimeLevel(String delayTimeLevel) {
		if (!MyFun.isEmpty(delayTimeLevel))
			this.delayTimeLevel = Integer.parseInt(delayTimeLevel);
	}

	/**
	 * @return the consumeBatchMaxSize
	 */
	public int get每批最多拉取() {
		return 每批最多拉取;
	}

	/**
	 * @param consumeBatchMaxSize the consumeBatchMaxSize to set
	 */
	public void set每批最多拉取(String 数量) {
		if (!MyFun.isEmpty(数量))
			this.每批最多拉取 = Integer.parseInt(数量);
	}

	/**
	 * @return the msgModel
	 */
	public MessageModel get消费模式() {

		return msgModel;
	}

	/**
	 * @param msgModel 设置集群模式 或 广播模式
	 */
	public void set消费模式(String 模式) {
		if (模式!=null&&模式.equals("广播"))
			this.msgModel = MessageModel.BROADCASTING;
		else
			this.msgModel = MessageModel.CLUSTERING;
	}

}
